package com.perdragon.controller;

import java.sql.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.perdragon.entity.Stipend;
import com.perdragon.service.StipendService;

@Controller
@RequestMapping("/stipend")
public class StipendController {
	@Autowired
	private StipendService stipendService;

	@RequestMapping("list")
	public ModelAndView listStipend(String page) {
		int pageNum = 1;
		if (page != null)
			pageNum = Integer.parseInt(page);
		int rowNum = stipendService.getRowNum();
		int maxPageNum = (int) Math.ceil(rowNum / 10.0);
		if (maxPageNum == 0)
			maxPageNum = 1;
		if (pageNum >= 1 && pageNum <= maxPageNum) {
			ModelAndView mv = new ModelAndView();
			mv.setViewName("stipend/listStipend");
			mv.addObject("stipendList", stipendService.listStipend((pageNum - 1) * 10));
			mv.addObject("pageNum", pageNum);
			mv.addObject("maxPageNum", maxPageNum);
			return mv;
		} else {
			ModelAndView mv = new ModelAndView("redirect:/stipend/list");
			return mv;
		}
	}

	@RequestMapping("/delete")
	public ModelAndView delete(String id) {
		stipendService.delete(Integer.parseInt(id));
		ModelAndView mv = new ModelAndView("redirect:/stipend/list");
		return mv;
	}

	@GetMapping("/add")
	public ModelAndView addStipend() {
		ModelAndView mv = new ModelAndView("stipend/addStipend");
		return mv;
	}

	@PostMapping("/add")
	public ModelAndView doAddStipend(String name, Float basic, Float eat, Float house, Float duty, Float scot,
			Float punishment, Float other, Date granttime) {
		Stipend stipend = new Stipend();
		stipend.setName(name);
		stipend.setBasic(basic);
		stipend.setEat(eat);
		stipend.setHouse(house);
		stipend.setDuty(duty);
		stipend.setScot(scot);
		stipend.setPunishment(punishment);
		stipend.setOther(other);
		stipend.setGranttime(granttime);
		Float totalize = basic + eat + house + duty - scot - punishment + other;
		stipend.setTotalize(totalize);
		stipendService.addStipend(stipend);
		ModelAndView mv = new ModelAndView("redirect:/stipend/list");
		return mv;
	}

	@GetMapping("/update")
	public ModelAndView updateStipend(String id) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("stipend/updateStipend");
		mv.addObject("s", stipendService.selectById(Integer.parseInt(id)));
		return mv;
	}

	@PostMapping("/update")
	public ModelAndView doUpdateStipend(String id, String name, Float basic, Float eat, Float house, Float duty,
			Float scot, Float punishment, Float other, Date granttime) {
		Stipend stipend = new Stipend();
		stipend.setId(Integer.parseInt(id));
		stipend.setName(name);
		stipend.setBasic(basic);
		stipend.setEat(eat);
		stipend.setHouse(house);
		stipend.setDuty(duty);
		stipend.setScot(scot);
		stipend.setPunishment(punishment);
		stipend.setOther(other);
		stipend.setGranttime(granttime);
		Float totalize = basic + eat + house + duty - scot - punishment + other;
		stipend.setTotalize(totalize);
		stipendService.updateStipend(stipend);
		ModelAndView mv = new ModelAndView("redirect:/stipend/list");
		return mv;
	}
}
